package com.db.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionToDB {
	// DataBase...
	private static Connection conn = null;

	/**
	 * @param args
	 */
	public static Connection connectToDb() {
		// DB Connection:
		System.out.println("MySQL Connect Example.");
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "laluvalera";
		String driver = "com.mysql.jdbc.Driver";
		String userName = "root";
		String password = "admin";
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager
					.getConnection(url + dbName, userName, password);
			System.out.println("Connected to the database");
			return conn;
		} catch (Exception e) {
			System.out.println("Error en la conextión");
			e.printStackTrace();
			return null;
		}
	}

	public static void closeConnection() {
		try {
			if (!conn.isClosed())
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
